package com.itany.boot.controller;

import com.github.pagehelper.PageInfo;
import com.itany.boot.constant.ResponseCodeConstant;
import com.itany.boot.entity.CourseType;
import com.itany.boot.exception.CourseTypeExistException;
import com.itany.boot.exception.LevelErrorException;
import com.itany.boot.exception.RequestParameterErrorException;
import com.itany.boot.service.CourseTypeService;
import com.itany.boot.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * @date 2025/10/13
 */
@RestController
@RequestMapping("/courseType")
public class CourseTypeController {

    @Autowired
    private CourseTypeService courseTypeService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(String pageNo, String pageSize){
        ModelAndView mav = new ModelAndView();
        PageInfo<CourseType> typePage = courseTypeService.findPage(pageNo, pageSize);
        mav.addObject("typePage", typePage);
        mav.setViewName("back_courseTypeSet");
        return mav;
    }

    @RequestMapping("/addType")
    public ResponseResult addType(String name){
        ResponseResult result = new ResponseResult();

        try {
            courseTypeService.addCourseType(name);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (CourseTypeExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/findById")
    public ResponseResult findById(String id){
        ResponseResult result = new ResponseResult();

        try {
            CourseType type = courseTypeService.findById(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            result.setReturnObject(type);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

    @RequestMapping("/modifyCourseType")
    public ResponseResult modifyCourseType(String id,String typeName){
        ResponseResult result = new ResponseResult();

        try {
            courseTypeService.modifyName(id,typeName);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (CourseTypeExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/modifyStatus")
    public ResponseResult modifyStatus(String id,String status){
        ResponseResult result = new ResponseResult();

        try {
            courseTypeService.modifyStatus(id,status);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/findChildByParentId")
    public ModelAndView findChildByParentId(String parentId, String pageNo, String pageSize) throws RequestParameterErrorException {
        ModelAndView mav = new ModelAndView("back_courseTypeSet");
        try {
            PageInfo<CourseType> typePage = courseTypeService.findChileByParentId(parentId, pageNo, pageSize);

            mav.addObject("typePage", typePage);
            mav.addObject("currentParentId", parentId);

            if (parentId != null) {
                CourseType parentCourse = courseTypeService.findById(parentId);
                mav.addObject("parentCourse", parentCourse);
            }
        } catch (RequestParameterErrorException e) {
            mav.addObject("errorMsg", e.getMessage());
        } catch (Exception e) {
            mav.addObject("errorMsg", "服务器内部异常");
            e.printStackTrace();
        }

        return mav;
    }

    @RequestMapping("/addChildType")
    public ResponseResult addChildType(String name,String parentId){
        ResponseResult result=new ResponseResult();
        try {
            courseTypeService.addChileType(name,parentId);
            result.setMessage("课程添加成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage()); // 显示"请求参数有误"
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (CourseTypeExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_LOGIN_DISABLED);
        } catch (LevelErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_LOGIN_DISABLED);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/backToParent")
    public ModelAndView backToParent(String currentParentId,String pageNo,String pageSize) throws RequestParameterErrorException {
        CourseType currentParent = courseTypeService.findById(currentParentId);
        String parentOfParentId = currentParent.getParentId() != null ? currentParent.getParentId().toString() : null;


        return findChildByParentId(parentOfParentId, pageNo, pageSize);
    }
}
